package com.sf.asp.controller;


import com.sf.asp.entity.User;
import com.sf.asp.entity.dto.DeleteDTO;
import com.sf.asp.entity.dto.PageDTO;
import com.sf.asp.entity.dto.UserDTO;
import com.sf.asp.entity.dto.UserQueryDTO;
import com.sf.asp.entity.vo.PageInfo;
import com.sf.asp.entity.vo.ResponseInfo;
import com.sf.asp.entity.vo.UserVO;
import com.sf.asp.service.RoleService;
import com.sf.asp.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jins
 * @since 2021-11-24
 */
@Controller
public class UserController {

    private final UserService userService;

    private final RoleService roleService;

    public UserController(UserService userService,RoleService roleService){
        this.userService = userService;
        this.roleService = roleService;
    }


    @PostMapping(value = "/user/save")
    @ResponseBody
    public ResponseInfo saveUser(@RequestBody UserDTO userParam){
        userService.saveUser(userParam);
        return ResponseInfo.ok();
    }

    @GetMapping(value = "/user")
    public String userPage(){
        return "/manage/user/list";
    }


    @GetMapping(value = "/user/save")
    public String userSavePage(ModelMap map){
        map.addAttribute("roles",roleService.listRoleForUser());
        return "manage/user/save";
    }

    @GetMapping(value = "/user/edit/{userId}")
    public String userEditPage(@PathVariable Integer userId,ModelMap map){
        UserVO userShow = userService.getUserById(userId);
        map.addAttribute("user",userShow);
        map.addAttribute("roles",roleService.listRoleForUser());
        return "manage/user/edit";
    }

    @GetMapping(value = "/users")
    @ResponseBody
    public PageInfo getUserList(UserQueryDTO  param){
        return userService.listUser(param);
    }


    @PostMapping(value = "/user/edit")
    @ResponseBody
    public ResponseInfo saveUser(UserVO userParam){
        userService.editUser(userParam);
        return ResponseInfo.ok();
    }

    @PostMapping(value = "/user/delete")
    @ResponseBody
    public ResponseInfo deleteUsers(@RequestBody DeleteDTO userIds){
        userService.deleteUsers(userIds);
        return ResponseInfo.ok();
    }

}
